// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  onLoad: function(options) {
    // 页面创建时执行
    this.login()
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },

  login(e){
    wx.login({
        success (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: 'http://127.0.0.1:8095/api/login/miniLogin',
              method:'POST',
              data: {
                loginType: 'wechat_mini',
                code: res.code
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
  },

  pay(e){
    wx.requestPayment({
        timeStamp: '1733640231',
        nonceStr: '22c4bd4a117b412bb184fc61768aa204',
        package: 'prepay_id=wx0814435160490303e5323c38c5d02e0001',
        signType: 'RSA',
        paySign: 'WGfvuM9VVx9cpNvExP+yHmQ2VYeJKR0K2m/KsVoTiY3Hus8D43AgKV3aRqadAP+kZkeFEoiNh/lN9z9dDWSEVXML/GsKvCsRSPa2SBjyyuiInWVSoxQePGO25WuXZFQNWE3LFb8WRlafh9uaPz9nhbIO3qEBsvUuy9eJPRX52NX6yas08z30a4vLXaw+21u+tcpaNg6pd4efCBoC+MkxzWMCC4YlCOsIPImGsPSZ3z+oj9TnFWunBs2mJwVxjHT2DMHrOzCha134dbqMoiE16rWgSUDl7bQOSd9PDjDuiCpg1QNcGUtKPjGsqXXwcKiG11pmAPy1ByPHYIk0pyy2Jw==',
        success (res) { 
            console.log('支付成功了')
            console.log(res)
        },
        fail (res) { }
      })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
